﻿using StudyEnglish.Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace StudyEnglish.ViewModel
{
    public class VolViewModel : INotifyPropertyChanged
    {
        private AppDataContext appDB;
        public VolViewModel(string appConnectionString)
        {
            appDB = new AppDataContext(appConnectionString);
        }
        private ObservableCollection<AppVocabulary> _lstVol;
        public ObservableCollection<AppVocabulary> LstVol
        {
            get { return _lstVol; }
            set
            {
                _lstVol = value;
                NotifyPropertyChanged("LstVol");
            }
        }
        private ObservableCollection<AppVocabulary> _lstVolForDay;
        public ObservableCollection<AppVocabulary> LstVolForDay
        {
            get { return _lstVolForDay; }
            set
            {
                _lstVolForDay = value;
                NotifyPropertyChanged("LstVolForDay");
            }
        }
        private ObservableCollection<AppVocabulary> _lstVolContainsWord;
        public ObservableCollection<AppVocabulary> LstVolContainsWord
        {
            get { return _lstVolContainsWord; }
            set
            {
                _lstVolContainsWord = value;
                NotifyPropertyChanged("LstVolContainsWord");
            }
        }

        private AppVocabulary _volByID;
        public AppVocabulary VolByID
        {
            get { return _volByID; }
            set
            {
                _volByID = value;
                NotifyPropertyChanged("VolByID");
            }
        }
        private int _countVolByWord;
        public int countVolByWord
        {
            get { return _countVolByWord; }
            set
            {
                _countVolByWord = value;
                NotifyPropertyChanged("countVolByWord");
            }
        }

        private int _volIdMax;
        public int VolIdMax
        {
            get { return _volIdMax; }
            set
            {
                _volIdMax = value;
                NotifyPropertyChanged("VolIdMax");
            }
        }

        public void saveChangeDB()
        {
            appDB.SubmitChanges();
        }

        public void GetLstVol()
        {
            var lst = (from s in appDB.Vol.AsEnumerable()
                          orderby Guid.NewGuid()
                          select s);
            _lstVol = new ObservableCollection<AppVocabulary>(lst);

        }

        public void GetLstVolForDay(int dayNum, int userID)
        {


            var lst = (from s in appDB.Daily
                          join p in appDB.Vol on s.VocabularyID equals p.ID
                          where (s.Day == dayNum && s.UserID == userID)
                          select p);
            _lstVolForDay = new ObservableCollection<AppVocabulary>(lst);

        }

        public void AddVol(AppVocabulary newVol)
        {
            appDB.Vol.InsertOnSubmit(newVol);
            appDB.SubmitChanges();

        }

        public void GetVolIDMax()
        {
           _volIdMax = appDB.Vol.Max(u => u.ID);
        }

        public void GetVolByWord(string word)
        {
            var count = (from s in appDB.Vol
                          where s.NewWord == word
                          select s).Count();
            _countVolByWord = count;
        }

        public void GetVolContainsWord(string word)
        {
            var lst = (from s in appDB.Vol
                           where s.NewWord.Contains(word)
                           select s);
            _lstVolContainsWord = new ObservableCollection<AppVocabulary>(lst);
        }

        public void GetVolByID(int ID)
        {
            var item = (from s in appDB.Vol
                           where s.ID == ID
                           select s).FirstOrDefault();
           // _volByID = new AppVocabulary();
            _volByID = item as AppVocabulary;
        }

        public void DelVol(AppVocabulary newVol)
        {
            appDB.Vol.DeleteOnSubmit(newVol);
            appDB.SubmitChanges();
        }

        public void UpdVol(AppVocabulary newVol)
        {
            // find a city to update
            var itemVol = from s  in appDB.Vol
                          where s.ID == newVol.ID
                                  select s;
            AppVocabulary old = itemVol.FirstOrDefault();

            old.NewWord = newVol.NewWord;
            old.Pronounciation = newVol.Pronounciation;
            old.Meaning = newVol.Meaning;
            appDB.SubmitChanges();

        }
        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion

    }
}
